package com.bjsxt.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.SysRole;
import com.bjsxt.model.Result;
import com.bjsxt.service.SysRoleService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotEmpty;

/**
 * description:
 * 角色管理控制层
 * 角色的权限:
 * 查询: sys_role_query
 * 修改: sys_role_update
 * 新增: sys_role_create
 * 删除: sys_role_delete
 *
 * @author ALIENWARE <br />
 * @date 2021/6/28
 */
@RestController
@RequestMapping("/roles")
@Api(tags = "角色管理的控制器")
public class SysRoleController {

    /**
     * 角色管理业务层接口
     */
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 分页查询角色数据
     *
     * @param page 分页对象<SysRole>
     * @param name 角色名称
     * @return 统一响应对象
     */
    @GetMapping
    @PreAuthorize("hasAuthority('sys_role_query')")
    @ApiOperation(value = "分页查询角色数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Long", paramType = "query", example = "20"),
            @ApiImplicitParam(name = "name", value = "角色名称", dataType = "String", paramType = "query", example = "管理员")
    })
    public Result<Page<SysRole>> findByPage(@ApiIgnore Page<SysRole> page, String name) {
        // 查询时间, 我们将最近新增的, 修改的数据优先展示, 排序 last_update_time
        page.addOrder(OrderItem.desc(SysRole.COL_LAST_UPDATE_TIME));
        // 调用业务层进行分页数据的查询
        Page<SysRole> sysPrivilegePage = sysRoleService.PageByName(page, name);
        // 返回响应数据
        return Result.success(sysPrivilegePage);
    }

    /**
     * 新增角色数据
     *
     * @param sysRole 角色数据对象
     * @return 统一响应对象
     */
    @PostMapping
    @PreAuthorize("hasAuthority('sys_role_create')")
    @ApiOperation(value = "新增角色数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRole", value = "SysRole 的 json 数据", dataType = "String", paramType = "body", example = "{}")
    })
    public Result<Void> add(@RequestBody @Validated SysRole sysRole) {
        // 调用业务层方法进行数据的保存
        boolean flag = sysRoleService.save(sysRole);
        // 如果为 true 新增成功
        if (flag) return Result.success("新增成功");
        // 如果为 false 新增失败
        return Result.fail("新增失败");
    }

    /**
     * 删除角色数据
     *
     * @param ids 角色 id 数组
     * @return 统一响应对象
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('sys_role_delete')")
    @ApiOperation(value = "删除角色数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "SysRole 的 id 集合", dataType = "Long", required = true, paramType = "body", example = "1")
    })
    public Result<Void> delete(@NotEmpty @RequestBody Long[] ids) {
        // 调用业务层方法进行数据的更新
//        boolean flag = sysRoleService.removeByIds(Arrays.asList(ids));
        boolean flag = sysRoleService.removeRoleByIds(ids);
        // 如果为 true 删除成功
        if (flag) return Result.success("删除成功");
        // 如果为 false 删除失败
        return Result.fail("删除失败");
    }
}
